<template>
  <div class="app-container">
    <div class="app-container-table-border">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px"
        class="top-left">
        <el-form-item label prop="reportDate">
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="～" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item label prop="ypCode">
          <el-input v-model="queryParams.ypCode" placeholder="请输入样品编号" clearable @keyup.enter.native="handleQuery"
            style="width: 180px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['syrw:bsDataReport:export']">导出</el-button>
        </el-col>
      </el-row>
      <el-table class="report-table" border stripe v-loading="loading" :data="dataList">
        <el-table-column label="序号" align="center" type="index" width="55px" />
        <el-table-column label="所属试验类型" align="center" prop="syTypeStr" min-width="180" show-overflow-tooltip />
        <el-table-column label="所属单位" align="center" prop="deptName" min-width="120" show-overflow-tooltip />
        <el-table-column label="取样地点" align="center" prop="ypVo.location" min-width="120" show-overflow-tooltip />
        <el-table-column label="取样时间" align="center" prop="ypVo.ypDate" min-width="120" show-overflow-tooltip />
        <el-table-column label="不合格品名称" align="center" prop="ypVo.yangpmc" min-width="120" show-overflow-tooltip />
        <el-table-column label="样品编号" align="center" prop="ypCode" min-width="220" show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="pointer theme-color" @click="doToHref(scope.row)">
              {{ scope.row.ypCode }}
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="不合格项目" align="center" prop="min" min-width="120" show-overflow-tooltip /> -->
        <el-table-column label="处理方法" align="center" prop="clff" min-width="120" show-overflow-tooltip />
        <el-table-column label="处理结果" align="center" prop="cljg" min-width="120" show-overflow-tooltip />
        <el-table-column label="备注" align="center" prop="min" min-width="120" show-overflow-tooltip />
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { getBhgtzList } from "@/api/statistics";
import Bus from "@/utils/bus";

export default {
  name: 'Bhgtz',
  data () {
    return {
      // 试验类型列表
      syTypeList: [],
      // 遮罩层
      loading: false,
      // 日期范围
      dateRange: [],
      // 显示搜索条件
      showSearch: true,
      showPopover: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        syType: undefined,
        deptId: undefined,
        reportName: undefined,
        reportDate: undefined,
        ypCode: undefined,
      },
      total: 0,
      dataList: [],
    };
  },
  // 监听顶部全局下拉
  computed: {
    visitedViews () {
      return this.$store.state.tagsView.visitedViews;
    },
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.handleQuery();
    }
  },
  created () {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.handleQuery();
  },
  methods: {
    /** 查询数据报告列表 */
    getList () {
      if (!this.queryParams.deptId) {
        this.$message.error("请先选择一个试验室");
        return;
      }
      this.loading = true;
      this.showPopover = false;
      getBhgtzList(this.addDateRange(this.queryParams, this.dateRange, 'ReportDate')).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$forceUpdate();
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    doToHref (row) {
      // 是否包含
      const isContain = this.visitedViews.some(item => {
        return item.name === "BsDataReport";
      });
      const info = {
        syType: row.syType,
        ypCode: row.ypCode
      };
      if (isContain) {
        Bus.$emit('setBsDataReport', info);
      };
      // ?syType=${this.queryParams.syType}&ypCode=${row.ypCode}
      const url = `/syrw/bsDataReport`;
      this.$router.push({ path: url, query: info });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "tjfx/exportBhgtz",
        {
          ...this.queryParams
        },
        `不合格台账_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped></style>
